package user

import (
	"github.com/gogf/gf/frame/gmvc"
	user "websshop/app/module/user"
	"websshop/app/service/captcha"
	userService "websshop/app/service/user"
	"websshop/app/utils/response"
)

type Controller struct {
	gmvc.Controller
}

func (c *Controller) Login()  {
	if userService.IsLogin(c.Session,"Admin") {
		userService.RedirectUrl(c.Response,"/admin/base/index")
	}

	if c.Request.Method == "POST" {
		var data *user.LoginUserArgs

		if err := c.Request.Parse(&data); err != nil {
			response.ErrorMsg(c.Request,err.Error())
		}
		if err := userService.AdminLogin(data,c.Session); err != nil {
			response.ErrorMsg(c.Request,err.Error())
		}
		response.JsonExit(c.Request,200,"登录成功","/admin/base/index")

	}else{
		id := captcha.GetCaptcha()
		c.View.Assign("captcha",id)
		c.View.Display("admin/user/login.html")
	}
}

func (c *Controller) Captcha()  {
	captcha.ShowCaptcha(c.Response.ResponseWriter,*c.Request)
}

func (c *Controller) ReloadCaptcha()  {
	
}

func (c *Controller) Logout() {
	if err := userService.Logout(c.Session,"Admin"); err != nil {

	}
	userService.RedirectUrl(c.Response,"/admin/user/login")
}

